﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CCheckboxData))]
    public class CCheckbox : CButton
    {
        #region fields

        private bool m_isChecked;
        private CStaticImage m_checkImage = null;

        #endregion

        #region properties

        /// <summary>
        /// Gets/Sets the checked status
        /// </summary>
        public bool IsChecked
        {
            get { return m_isChecked; }
            set
            {
                if (value == true)
                    m_children[m_name + "_check"].Show();
                else
                    m_children[m_name + "_check"].Hide();
                m_isChecked = value;
            }
        }

        /// <summary>
        /// Gets/Sets the CStaticImage representing the check icon
        /// </summary>
        /// <seealso cref="CStaticImage"/>
        public CStaticImage CheckImage
        { get { return m_checkImage; } set { m_checkImage = value; } }

        #endregion

        #region ctor

        public CCheckbox(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            m_isChecked = false;

            this.m_releasedTexture = HUD.Skinner.GetTexture("checkbox");
            this.m_texture = this.m_releasedTexture;

            m_checkImage = new CStaticImage(hud, this, name + "_check");
            m_checkImage.Image = HUD.Skinner.GetTexture("checkboxicon");
            m_checkImage.Size = new Vector2(0.8f, 0.8f);
            m_checkImage.Position = new Vector2(0.1f, 0.1f);
            m_checkImage.Hide();

            Size = new Vector2(20, 20);
        }

        #endregion

        #region events

        /// <summary>
        /// Method called whenever we click on the checkbox
        /// </summary>
        public void check(CWidget sender, CMouse mouse)
        {
            if (mouse.ButtonReleasedEvent.First() == CMouse.EButton.LEFT)
                (sender as CCheckbox).IsChecked = !(sender as CCheckbox).IsChecked;
        }

        /// <summary>
        /// Method called whenever the mouse is pressed and is over this widget
        /// </summary>
        public override bool onClick(CMouse mouse)
        {
            if (m_clickedTexture != null)
            {
                m_clickedTexture.Rectangle = m_texture.Rectangle;
                this.m_texture = m_clickedTexture;
            }
            InvokeClick(this);
            IsLeftClicked = true;
            return true;
        }

        /// <summary>
        /// Method called whenever the mouse is released and is over this widget and was pressed over this widget
        /// </summary>
        public override bool onRelease(CMouse mouse)
        {
            if (IsLeftClicked && isMouseOver(mouse))
            {
                check(this, mouse);
                if (m_releasedTexture != null)
                    this.m_texture = m_releasedTexture;
                InvokeRelease(this);
                IsLeftClicked = false;
                return true;
            }
            return false;
        }

        #endregion

    }
}
